package com.stiff.web.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.stiff.common.service.ApiService;
import com.stiff.common.service.RedisService;
import com.stiff.manage.pojo.ItemDesc;
import com.stiff.web.bean.Item;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class ItemService {
    @Value("${TAOTAO_MANAGE_URL}")
    private String TAOTAO_MANAGE_URL;
    @Value("${ITEM_URL}")
    private String ITEM_URL;
    @Value("${ITEM_DESC_URL}")
    private String ITEM_DESC_URL;
    @Value("${ITEM_PARAM_URL}")
    private String ITEM_PARAM_URL;
    @Autowired
    private ApiService apiService;
    @Autowired
    private RedisService redisService;

    private static final ObjectMapper MAPPER = new ObjectMapper();
    public static final String REDIS_KEY = "TAOTAO_WEB_ITEM_DETAIL_";
    private static final Integer REDIS_TIME = 60*60*24;
    /**
     * 通过商品ID获取商品信息
     * @param itemId 商品ID
     * @return com.stiff.web.bean.Item
     */
    public Item queryItemById(Long itemId) {
        //从缓存中命中
        String key = REDIS_KEY + itemId;
        try {
            String cacheData = this.redisService.get(key);
            if(StringUtils.isNotEmpty(cacheData)){
                return this.MAPPER.readValue(cacheData, Item.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            String url = TAOTAO_MANAGE_URL+ITEM_URL+"/"+itemId;
            String jsonData = this.apiService.doGet(url);
            if(StringUtils.isEmpty(jsonData)){
                return null;
            }
            //将结果集写入缓存
            try {
                this.redisService.set(key,jsonData,REDIS_TIME);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return this.MAPPER.readValue(jsonData, Item.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过商品ID获取商品描述信息
     * @param itemId 商品ID
     * @return com.stiff.manage.pojo.ItemDesc
     */
    public ItemDesc queryItemDescById(Long itemId) {
        try {
            String url = TAOTAO_MANAGE_URL+ITEM_DESC_URL+"/"+itemId;
            String jsonData = this.apiService.doGet(url);
            if(StringUtils.isEmpty(jsonData)){
                return null;
            }
            return this.MAPPER.readValue(jsonData, ItemDesc.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过商品ID获取商品规格信息
     * @param itemId 商品ID
     * @return 数据与HTML标签拼接的字符串
     */
    public String queryItemParamByItemId(Long itemId) {
        try {
            String url = TAOTAO_MANAGE_URL+ITEM_PARAM_URL+"/"+itemId;
            String jsonData = this.apiService.doGet(url);
            //解析json
            if(StringUtils.isEmpty(jsonData)){
                return null;
            }
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            ArrayNode paramNode = (ArrayNode)MAPPER.readTree(jsonNode.get("paramData").asText());
            StringBuilder sb = new StringBuilder();
            sb.append("<table cellpadding=\"0\" cellspacing=\"l\" width=\"100%\" border=\"0\"class=\"Ptable\"><tbody>");
            for(JsonNode param:paramNode){
                sb.append("<tr><th class=\"tdTitle\" colspan=\"2\">"+param.get("group").asText()+"</th></tr>");
                ArrayNode params = (ArrayNode)param.get("params");
                for(JsonNode p:params){
                    sb.append("<tr><td class=\"tdTitle\">"+p.get("k").asText()+"</td><td>"+p.get("v").asText()+"</td></tr>");
                }
            }
            sb.append("</tbody></table>");
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}